package com.example.demo.controller;

import com.example.demo.dto.ApiResponse;
import com.example.demo.entity.Reservation;
import com.example.demo.entity.User;
import com.example.demo.entity.ReservationStatus;
import com.example.demo.service.ReservationService;
import com.example.demo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/reservations")
@RequiredArgsConstructor
public class ReservationController {

    private final ReservationService reservationService;

    private final UserService userService;

    @PostMapping
    public ApiResponse<?> createReservation(@AuthenticationPrincipal User user, @RequestBody Reservation reservation) {
        return ApiResponse.success("预约创建成功", reservationService.createReservation(user.getId(), reservation));
    }

    @PutMapping("/{id}")
    public ApiResponse<?> updateReservation(@PathVariable Long id, @RequestBody Reservation reservation) {
        reservation.setId(id);
        return ApiResponse.success("预约更新成功", reservationService.updateReservation(reservation));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> deleteReservation(@PathVariable Long id) {
        reservationService.deleteReservation(id);
        return ApiResponse.success("预约删除成功");
    }

    @GetMapping("/{id}")
    public ApiResponse<?> getReservationById(@PathVariable Long id) {
        return ApiResponse.success(reservationService.getReservationById(id));
    }

    @GetMapping("/student/{studentId}")
    public ApiResponse<?> getReservationsByStudent(@PathVariable Long studentId) {
        ApiResponse<?> userResponse = userService.getUserById(studentId);
        if (!userResponse.isSuccess()) {
            return ApiResponse.error("学生不存在");
        }
        User student = (User) userResponse.getData();
        return reservationService.getReservationsByStudent(student);
    }

    @GetMapping("/status/{status}")
    public ApiResponse<?> getReservationsByStatus(@PathVariable ReservationStatus status) {
        return ApiResponse.success(reservationService.getReservationsByStatus(status));
    }

    @GetMapping("/approver/{approverId}")
    public ApiResponse<?> getReservationsByApprover(@PathVariable Long approverId) {
        ApiResponse<?> userResponse = userService.getUserById(approverId);
        if (!userResponse.isSuccess()) {
            return ApiResponse.error("审批人不存在");
        }
        User approver = (User) userResponse.getData();
        return reservationService.getReservationsByApprover(approver);
    }

    @GetMapping("/date-range")
    public ApiResponse<?> getReservationsByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end) {
        return ApiResponse.success(reservationService.getReservationsByDateRange(start, end));
    }

    @PutMapping("/{id}/approve")
    public ApiResponse<?> approveReservation(@PathVariable Long id) {
        return ApiResponse.success("预约审批通过", reservationService.approveReservation(id));
    }

    @PutMapping("/{id}/reject")
    public ApiResponse<?> rejectReservation(@PathVariable Long id) {
        return ApiResponse.success("预约已拒绝", reservationService.rejectReservation(id));
    }

    @PutMapping("/{id}/cancel")
    public ApiResponse<?> cancelReservation(@PathVariable Long id) {
        return ApiResponse.success("预约取消成功", reservationService.cancelReservation(id));
    }

    @GetMapping("/user")
    public ApiResponse<?> getUserReservations(@AuthenticationPrincipal User user) {
        return ApiResponse.success(reservationService.getUserReservations(user.getId()));
    }

    @GetMapping("/lab/{labId}")
    public ApiResponse<?> getLabReservations(@PathVariable Long labId) {
        return ApiResponse.success(reservationService.getLabReservations(labId));
    }
} 